<?php
namespace app\index\controller;
use think\Db;
use think\Controller;
use app\api\controller\Index;
use app\index\controller\PaymentController;
class MemberController extends BaseController
{	
	protected function _initialize() {
	   $type=get_brower_type();
	   $this->assign('browder_type',$type);
	   $user_info = db('member')->alias("a")->join('member b',"a.pid1=b.id","left")
	   ->where('a.id',session('user_info')['id'])
	   ->field("a.*,b.nick_name as pname,b.real_name as prname")
	   ->find();
	   $this->assign("pname",$user_info['prname']?$user_info['prname']:$user_info['pname']);
		if($user_info['is_city']==1){
			$this->assign("jsname",'匠心合伙人');
		}elseif($user_info['is_pt']==1){
			$this->assign("jsname",'美丽合伙人');
		}elseif($user_info['is_rz']==1&&$user_info['is_qy']){
			$this->assign("jsname",'美丽代言人');
		}elseif($user_info['is_rz']==1){
			$this->assign("jsname",'星创会员');
		}else{
			$this->assign("jsname",'注册会员');
		}
		
		
		
		session("user_info",$user_info);
       parent::_initialize();
    }
	//会员排行榜
	function mem_list(){
		$date=date("Y-m");
		$this->assign("date",$date);
		$where['is_rz']=1;
		
		$order=" b desc";
		$type=2;
		$type_name="按奖励排序";
		if(input("order")){
			$order=" c desc";
			$type=0;
			$type_name="按会员排序";
		}
		$field="a.nick_name,a.id";
		$b=db("member")->where("pid1=a.id or pid2=a.id")->wheretime("add_time","month")->field("count(id)")->buildSql();
		$c=db("gdyj_record")->where("user_id=a.id")->wheretime("add_time","month")->field("sum(balance)")->buildSql();
		
		$field.=",($b) as b";
		$field.=",($c) as c ";
		$this->assign('type',$type);
		$this->assign('type_name',$type_name);
		$list=db("member")->alias("a")
		->where($where)->field($field)->order("$order")->limit(50)->select();
		$this->assign("list",$list);
		return $this->fetch();
	}
	//合伙人排行榜
	function pt_list(){
		$date=date("Y-m");
		$this->assign("date",$date);
		$where['is_pt']=1;
		$where['is_city']=2;
		
		$order=" b desc";
		$type=2;
		$type_name="按奖励排序";
		if(input("order")){
			$order=" c desc";
			$type=0;
			$type_name="按业绩排序";
		}
		$this->assign('type',$type);
		$this->assign('type_name',$type_name);
		$field="a.nick_name,a.id";
		$b=db("member")->where("pt_id=a.id")->wheretime("add_time","month")->field("count(id)")->buildSql();
		$c=db("gdyj_record")->where("user_id=a.id")->wheretime("add_time","month")->field("sum(balance)")->buildSql();
		
		$field.=",($b) as b";
		$field.=",($c) as c ";
		$this->assign('type',$type);
		$this->assign('type_name',$type_name);
		$list=db("member")->alias("a")
		->where($where)->field($field)->order("$order")->limit(50)->select();
		$this->assign("list",$list);
		return $this->fetch();
	}
	//合伙人订货
	function dinghuo(){
		if($_POST){
			$data=$_POST;
			$data['add_time']=now_time();
			$data['user_id']=session("user_info")['id'];
			$data['city_id']=session("user_info")['city_id'];
			$res=db('dinghuo')->insert($data);
			if($res){
				return json(['code'=>200,'msg'=>'提交成功']);
			}else{
				return json(['code'=>400,'msg'=>'提交失败']);
			}
		}
		return $this->fetch();
	}
		//运营中心订货
	function ct_dinghuo(){
		if($_POST){
			$data=$_POST;
			$data['add_time']=now_time();
			$data['user_id']=session("user_info")['id'];
			$data['city_id']=session("user_info")['city_id'];
			$data['type']=2;
			$res=db('dinghuo')->insert($data);
			if($res){
				return json(['code'=>200,'msg'=>'提交成功']);
			}else{
				return json(['code'=>400,'msg'=>'提交失败']);
			}
		}
		return $this->fetch();
	}
	//申请成为合伙人
	function sq_hhr(){
		if($_POST){
			$data=$_POST;
			$data['add_time']=now_time();
			$data['status']=1;
			$data['user_id']=session("user_info")['id'];
			$data['city_id']=session("user_info")['city_id'];
			$res=db('pt_sq')->insert($data);
			if($res){
				return json(['code'=>200,'msg'=>'申请成功']);
			}else{
				return json(['code'=>400,'msg'=>'申请失败']);
			}
		}
		$sh=db('pt_sq')->where('user_id',session("user_info")['id'])->where("type",1)->find();
		return $this->fetch('',['sh'=>$sh]);
	}
	//申请成为城市运营商
	function sq_city(){
		if($_POST){
			$data=$_POST;
			$data['add_time']=now_time();
			$data['status']=1;
			$data['user_id']=session("user_info")['id'];
			$data['city_id']=session("user_info")['city_id'];
			$data['type']=2;
			$res=db('pt_sq')->insert($data);
			if($res){
				return json(['code'=>200,'msg'=>'申请成功']);
			}else{
				return json(['code'=>400,'msg'=>'申请失败']);
			}
		}
		$sh=db('pt_sq')->where('user_id',session("user_info")['id'])->where('type',2)->find();
		return $this->fetch('',['sh'=>$sh]);
	}
	//优惠券
	function quan(){
		$status=input("status")?input('status'):0;
		$this->assign('status',$status);
		return $this->fetch();
	}
	//会员卡
	function mcard(){
		return $this->fetch();
	}
	//团队直接推荐
	function zj_tj(){
		$user_info = session("user_info");
		$user_id = $user_info['id'];
		
		$count1 = db('member')->where('pid1',$user_id)->count();
		$count2 = db('member')->where('pid2',$user_id)->count();
		$this->assign('count1',$count1);
		$this->assign('sum',$count1+$count2);
		
		$w['pid1'] = $user_id;
		if(input('name')){
			$w['id|mobile|nick_name'] = ['like','%'.input('name').'%'];
		}
		if($_POST){
			$list = db('member')->field('id,nick_name,mobile,total_xf,add_time')->where($w)->order("add_time desc")->page(input("page"),10)->select();
			return json(['list'=>$list]);
		}		
		
		return $this->fetch();
	}
	//团队二级推荐
	function jj_tj(){
		$user_info = session("user_info");
		$user_id = $user_info['id'];
		
		$count1 = db('member')->where('pid1',$user_id)->count();
		$count2 = db('member')->where('pid2',$user_id)->count();
		$this->assign('count2',$count2);
		$this->assign('sum',$count1+$count2);
		
		$w['pid2'] = $user_id;
		if(input('name')){
			$w['id|mobile|nick_name'] = ['like','%'.input('name').'%'];
		}
		if($_POST){
			$list = db('member')->field('id,nick_name,mobile,total_xf,add_time')->where($w)->order("add_time desc")->page(input("page"),10)->select();
			return json(['list'=>$list]);
		}		
		
		return $this->fetch();
	}
	function yuding(){
		$w['a.user_id']=session("user_info")['id'];
		if($_POST){
			$agent_info = db('reserve')->alias("a")->join("beaut_home b","b.id=a.home_id")->where($w)->order("a.add_time desc")->field("a.*,b.home_name")->page(input("page"),10)->select();
			return json(['list'=>$agent_info]);
		}
		return $this->fetch();
	}
	function tixian(){
		$user=db('member')->where("id",session("user_info")['id'])->find();
		return $this->fetch('',['info_user'=>$user]);
	}
	//可提现佣金
	function dtx_yj(){
		$w['id']=session("user_info")['id'];
		$w1['tj_id|tj_id2']=session("user_info")['id'];
		$w1['status']=['in',[2,3]];
		if($_POST){
			$page=input("page");
			$begin=($page-1)*20;
			$field="user_id,add_time,tj_yj,tj_id,tj_id2,tj_yj2,pt_id,pt_yj,ct_id,ct_yj";
			$agent_info = db('good_order')->where($w1)->order("add_time desc")->limit($begin,20)->select();
			return json(['list'=>$agent_info,'user_id'=>session("user_info")['id']]);
		}
		
		return $this->fetch('');
	}
	//总佣金
	function yjs(){
		$w1['tj_id']=session("user_info")['id'];
		$w1['status']=['in',[2,3,4]];
		if($_POST){
			$page=input("page");
			$begin=($page-1)*20;
			$field="user_id,add_time,tj_yj";
			if(input("score_tp")==1){
				$w1['status']=['in',[4]];
			}elseif(input("score_tp")==2){
				$w1['status']=['in',[2,3]];
			}
			if(input("time_tp")==1){
				$list=db('good_order')->where($w1)->whereTime('add_time', 'week')->order('add_time desc')->limit($begin,20)->select();
				$sum=db('good_order')->where($w1)->whereTime('add_time', 'week')->sum('tj_yj');
			}elseif(input("time_tp")==2){
				$list=db('good_order')->where($w1)->whereTime('add_time', 'month')->order('add_time desc')->limit($begin,20)->select();
				$sum=db('good_order')->where($w1)->whereTime('add_time', 'month')->sum('tj_yj');
			}elseif(input("time_tp")==3){
				$list=db('good_order')->where($w1)->whereTime('add_time', 'year')->order('add_time desc')->limit($begin,20)->select();
				$sum=db('good_order')->where($w1)->whereTime('add_time', 'year')->sum('tj_yj');
			}elseif(input("time_tp")==4){
				$list=db('good_order')->where($w1)->whereTime('add_time', 'yeah')->order('add_time desc')->limit($begin,20)->select();
				$sum=db('good_order')->where($w1)->whereTime('add_time', 'year')->sum('tj_yj');
			}else{
				$list=db('good_order')->where($w1)->order('add_time desc')->limit($begin,20)->select();
				$sum=db('good_order')->where($w1)->sum('tj_yj');
			}
			if(!$sum){
				$sum=0;
			}
			return json(['list'=>$list,'sum'=>$sum]);
		}
		return $this->fetch('');
	}
	//可提现佣金
	function yj(){
		return $this->fetch();
	}
	
	//佣金提现
	function yj_tixian(){
		$user_id = session("user_info")['id'];
		$user_info = Db::table('cz_member')->where('id',$user_id)->find();		
		$ktxyj = $user_info['yj'];	
		if($ktxyj<input('txyj')){
			return json(['code'=>402,'msg'=>'提现佣金不足']);
		}
		
        $data['user_id'] = $user_id;
        $data['money'] = input('txyj');
        $data['status'] = 1;
		$data['city_id']=session("user_info")['city_id'];
        $data['create_time'] = now_time();
        $result = Db::table('cz_member_tixian')->insert($data);
		if($result){
			gdyj_record_log($user_id,-input('txyj'),"提现扣除佣金",18,'',$user_info['pt_id'],$user_info['city_id']);
			//扣除佣金
			$rest = db('member')->where('id', $user_id)->setDec('yj', input('txyj'));
			
			return json(['code'=>200,'msg'=>'申请成功']);
		}else{
			return json(['code'=>402,'msg'=>'申请失败']);
		}

		return $this->fetch();
	}	
	
	function sk(){
		$code_url="http://".$_SERVER['HTTP_HOST']."/index/member/show_sk?fromuid=".session('user_info')['id'];
		$this->assign('logo',session('user_info')['logo']);
		$this->assign('code_url',$code_url);
		return  $this->fetch();
	}
	function show_sk(){
		$fromuid=input("fromuid");
		$this->assign('fromuid',$fromuid);
		$this->assign('user_info',session("user_info"));
		return $this->fetch();
	}
	//奖励金
	function jlj(){
		$user_info=session("user_info");
		$w['user_id']=$user_info['id'];
		$config =get_config('ticket_config');
		//$w['type']=5;
		if($_POST){
			$list = db('hb_list')->field('id,message,balance,add_time')->where($w)->order("add_time desc")->page(input("page"),10)->select();
			return json(['list'=>$list]);
		}
		$field="count(id) as c,sum(balance) s";
		$log=db('hb_list')->where($w)->field($field)->find();
		if(!$log){
			$log['c']=0;
			$log['s']=0;
		}
		
		
		
		$this->assign("config",$config);
		$this->assign('log',$log);
		return $this->fetch('');
	}
	//会员申请页面
	function user(){
		return $this->fetch();
	}
	//自定义分享设置
	function share_config(){
		return share_config();
	}
	function glj_sysm(){
		return $this->fetch();
	}
	public function card(){
		$user=session("info_user");
		if(!$user['no']){
			$card=get_user_no($user['id']);
			$user['no']=$card;
			session("info_user")['no']=$card;
			db("info_user")->where("id",$user['id'])->update(["no"=>$card]);
			$user=db('info_user')->where("id",$user['id'])->find();
			session('info_user',$user);
		}
		return $this->fetch();
	}

	//我的推荐列表
	public function tj_list(){
		if($_POST){
			$info_user = session('user_info');
			$page=1;
			$where['user_id']=$info_user['id'];
			if(input("page")){
				$page=input("page");
			}
			$begin=($page-1)*10;
			$member = db('member')->where("pid1",$info_user['id'])->order('add_time desc')->limit($begin,10)->select();
			
			if($member){
				return json(['code'=>200,"list"=>$member]);
			}else{
				return json(['list'=>null]);
			}
		}
		return $this->fetch();
	}
	//消息中心
	public function msg_list(){
		if($_POST){
			$info_user = session('user_info');
			$page=1;
			$where['user_id']=$info_user['id'];
			if(input("page")){
				$page=input("page");
			}
			$begin=($page-1)*10;
			$member = db('message')->where("user_id",$info_user['id'])->order('add_time desc')->limit($begin,10)->select();
			if($member){
				return json(['code'=>200,"list"=>$member]);
			}else{
				return json(['list'=>null]);
			}
		}
		return $this->fetch();
	}
	//红包记录
	public function hb_list(){
		if($_POST){
			$info_user = session('user_info');
			$page=1;
			$where['user_id']=$info_user['id'];
			if(input("page")){
				$page=input("page");
			}
			$begin=($page-1)*10;
			$where['user_id'] = $info_user['id'];
			if($info_user['is_pt']==1){
				$where['type'] = ['in','2,3'];
			}else{
				$where['type'] = '1';
			}
			$logs=db('hb_list')->where($where)->order('add_time desc')->limit($begin,10)->select();
			if($logs){
				return json(['code'=>200,"list"=>$logs]);
			}else{
				return json(['list'=>null]);
			}
		}
		return $this->fetch();
	}
	
	//佣金记录
	public function yj_list(){
		if($_POST){
			$info_user = session('user_info');
			$page=1;
			$where['user_id']=$info_user['id'];
			if(input("page")){
				$page=input("page");
			}
			$begin=($page-1)*10;
			$where['user_id'] = $info_user['id'];
			if($info_user['is_pt']==1){
				$where['type'] = '4';
			}else{
				$where['type'] = '5';
			}
			$logs=db('hb_list')->where($where)->order('add_time desc')->limit($begin,10)->select();
			if($logs){
				return json(['code'=>200,"list"=>$logs]);
			}else{
				return json(['list'=>null]);
			}
		}
		return $this->fetch();
	}
	public function yindao(){
		return $this->fetch();
	}
	//获取token
	public function getToken($we_config){
		$cache = cache('access_token');
		if ($cache && $cache['token'] && $cache['expire'] > time()) {
			return $cache['token'];
		}
		/*$appid=$we_config['wxpay_appid'];
		$appsecret=$we_config['wxpay_appsercet'];*/
		$appid=config('wxpay_appid');
		$appsecret=config('wxpay_appsercet');
		$rurl="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
		$weixin =  file_get_contents($rurl);
		$jsondecode = json_decode($weixin); //对JSON格式的字符串进行编码
		$array = get_object_vars($jsondecode);//转换成数组
		if($array['access_token']){
			$record = array();
			$record['token'] = $array['access_token'];
			$record['expire'] = time() + 7000;
			cache('access_token', $record, $record['expire']);
			return $array['access_token'];
		}else{
			return json(['code'=>406,'msg'=>'获取微信公众号授权失败！错误代码:'.$array['errcode']]);
		}		
	}

	public function about(){
			return  $this->fetch();
	}
	
	//我的推广会员
    public function tglist(){
    	$info_user = session('user_info');
    	$list=db('info_user')->where('level',$info_user['id'])->field("id,user_name,mobile,score")->order('add_time desc')->select();
		$this->assign('list',$list);
       	return  $this->fetch();
    }
    //会员资料
    public function member()
    {
    	$info_user = session('user_info');
    	
    	if(request()->isPost()){
    		$param=input('post.');
    		$rest=db('member')->where('id',$info_user['id'])->update($param);
    		if($rest){
    			$this->success('修改成功');
			}else{
				$this->error('修改失败');
			}
    	}
    	
    	$info_user = db('member')->where('id',$info_user['id'])->find();
    	
    	$this->assign('info_user',$info_user);
       	return  $this->fetch();
    }
    
    //推广中心
    public function friends()
    {	
    	//二维码链接
    	$code_url="http://".$_SERVER['HTTP_HOST']."/index/member/regist?fromuid=".session('user_info')['id'];
		$use=db("member")->where("open_id",session("openid"))->find();
		$this->assign('logo',$use['logo']);
		$this->assign('code_url',$code_url);
		return  $this->fetch("ewm");
    }
    
    function open_fx(){
    	$info_user = session('user_info');
		$cz_type=2;
		if(input("cz_type")){
			$cz_type=input("cz_type");
		}
		$this->assign('cz_type',$cz_type);
    	$info_user = db('info_user')->where('id',$info_user['id'])->find();
    	$this->assign('info_user',$info_user);
		
		return  $this->fetch();	
    }
    //暂无数据
    function none_style(){
    	return  $this->fetch();	
    }
	public function get_trades(){
		$data=db('sys_platform_type')->where('pid',input("trade_id"))->select();
		return json(["data"=>$data]);
	}
	
	 /**
     * 获取验证码
     * 注意防盗刷
     * 
     */
    function verify_code(){
    	
    	if(request()->isPost()){
    		return get_code($_POST['mobile'],1);
    	}
    }
	
	//注册
    public function regist()
    {
    	$user=db('member')->where("open_id",session('openid'))->find();
		if($user){		
			$this -> redirect('index');
		}
    	if(request()->isPost()){
    		$result = check_sendcode($_POST['mobile'], $_POST['yzm'], '发送成功');
			if($result != 1) {
				$this->error('验证码错误或过期');
			}			
			$ch_where['mobile'] = $_POST['mobile'];
    		$user_info = db('member')->where($ch_where)->find();
    		//检查手机号码
	    	if($user_info){
	    		$this->error('该手机号码已经注册');
	   		}
			if(!session("openid")){
				$this->error('获取open_id失败');
			}
		
			$data['mobile'] = $_POST['mobile'];
			$data['open_id'] = session("openid");
			$this->get_nick(session("openid"), $data);
			if($_POST['tj_id']){
				$data['pid1'] = $_POST['tj_id'];
				//检查推荐人
				$tjinfo = db('member')->where('id',$_POST['tj_id'])->find();
		    	if(!$tjinfo && $_POST['tj_id']){
		    		$this->error('该推荐人不存在');
		   		}else{
		   			$data['pid2'] = $tjinfo['pid1'];
		   			$data['pt_id'] = $tjinfo['pt_id'];
					$data['old_pt'] = $tjinfo['pt_id'];
					$data['city_id']=$tjinfo['city_id'];
		   		}
			}else{
				$data['pid2'] = 3;//推荐人默认为1
				$data['pid1'] = 3;//推荐人默认为1
				$data['pt_id'] = 3;//合伙人默认为1
				$data['old_pt'] = 3;//关系合伙人默认为1
				$data['city_id'] =3;//关系合伙人默认为1			
			}
			$data['nick_name']=session("wx_user")['nick_name'];
			$data['logo']=session("wx_user")['headimgurl'];
			$data['add_time'] = now_time();
			$rest = db("member")->insert($data);
			if($rest){
				$user_id = db('member')->getLastInsID();
				$user_info = db('member')->where('id',$user_id)->find();
				session('user_info',$user_info);
				session('user_id',$user_id);
				return json(['code'=>200,'msg'=>'注册成功']);
			}else{
				$this->error('注册失败');
			}			
    	}else{
    		$this->assign('fromuid',input('fromuid'));
    	}
       return  $this->fetch();
    }
	public function get_nick($openid,&$data){
		$appid=config('wxpay_appid');
		$secret=config('wxpay_appsercet');
		$url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret";	
		$res=file_get_contents($url);
		$jsondecode1 = json_decode($res); //对JSON格式的字符串进行编码
		$array1 = get_object_vars($jsondecode1);//转换成数组
		$access_token=$array1['access_token'];
		$res=file_get_contents("https://api.weixin.qq.com/cgi-bin/user/info?access_token=$access_token&openid=$openid&lang=zh_CN");
		$jsondecode1 = json_decode($res); //对JSON格式的字符串进行编码
		$array1 = get_object_vars($jsondecode1);//转换成数组
		if(isset($array1['nickname'])){
			$data['nick_name']=$array1['nickname'];
			$data['logo']=$array1['headimgurl'];
			$data['is_atten']=1;
			
		}
		
	}
	//会员
	public function huiyuan(){
		return  $this->fetch();
	}
	//会员中心
    public function index()
    {
       if(!session("user_info")){
	   	$this -> redirect("regist");
	   }  	
//  	$info_user = session('user_info');
//		if(!$info_user['logo']){
//			parent::up_user(session("openid"));
//		}
		$info_user = session('user_info');
		$myfriends=db("member")->where("pid1",$info_user['id'])->count();
		$this->assign("counts",$myfriends);
		
		$ptcount=db("member")->where("old_pt",$info_user['id'])->count();
		$this->assign("ptcount",$ptcount);
    	$this->assign('info_user',$info_user);
		
		$tjname = db("member")->where("id",$info_user['pid1'])->find();
		if($tjname){
			$this->assign("tjname",$tjname['mobile']);
		}

		
		//红包、奖励金
		$where['user_id'] = $info_user['id'];
		if($info_user['is_pt']==1){
			$jlsum = db('hb_list')->where($where)->where('type','in','2,3')->sum('balance');
			$yjsum = db('hb_list')->where($where)->where('type','4')->sum('balance');
		}else{
			$jlsum = db('hb_list')->where($where)->where('type','1')->sum('balance');
			$yjsum = db('hb_list')->where($where)->where('type','5')->sum('balance');			
		}
		
		//获取当前用户订单
		$order_list = db('good_order')->where('user_id',session('user_id'))->select();
		
		$dfk_num = 0;
		$dfh_num = 0;
		$dsh_num = 0;
		$dpj_num = 0;
		foreach($order_list as $key=>$value){
			if($value['status'] == 1){
				$dfk_num = $dfk_num+1;
			}
			
			if($value['status'] == 2){
				$dfh_num = $dfh_num+1;
			}
			
			if($value['status'] == 3){
				$dsh_num = $dsh_num+1;
			}
			
			/*if($value['status'] == 4){
				$dpj_num = $dpj_num+1;
			}*/
		}
		$order_num['dfk_num'] = $dfk_num;
		$order_num['dfh_num'] = $dfh_num;
		$order_num['dsh_num'] = $dsh_num;
		//$order_num['dpj_num'] = $dpj_num;
		
		$this->assign("order_num",$order_num);
		$this->assign("jlsum",$jlsum);
		$this->assign("yjsum",$yjsum);
       	return  $this->fetch();
    }		
	//合伙人
	public function hehuoren(){
		$info_user = session('user_info');
		if(request()->isPost()){
			$parm['is_pt'] = 3;  
       	 	$result = db('member')->where('id',$info_user['id'])->update($parm);
	        if($result){
				return json(['code'=>200,'msg'=>'申请成功']);
	        }else{
	       		return json(['code'=>402,'msg'=>'申请失败']);
	        }
		}
		
		$userinfo = db('member')->where('id',$info_user['id'])->find();
		$this->assign('is_pt',$userinfo['is_pt']);
		return  $this->fetch();
	}
	
	//服务协议
	public function feedback(){
		return  $this->fetch();
	}
	
	//我的推荐合伙人列表
	public function tjpt_list(){
		if($_POST){
			$tjcount = db('member')->field('pt_id,count(id) as count')->group('pt_id')->buildSql();
			
			$info_user = session('user_info');
			$page=1;
			$where['user_id']=$info_user['id'];
			if(input("page")){
				$page=input("page");
			}
			$begin=($page-1)*10;

			$member = db('member')->alias('a')
			->join($tjcount." b","a.id=b.pt_id",'LEFT')
			->field('a.*,b.count')
			->where("a.old_pt",$info_user['id'])->order('a.add_time desc')->limit($begin,10)->select();			
			if($member){
				return json(['code'=>200,"list"=>$member]);
			}else{
				return json(['list'=>null]);
			}
		}
		return $this->fetch();
	}
	
	//运营中心
	public function yunying(){
		$info_user = session('user_info');
		if(request()->isPost()){
			$parm['is_city'] = 3;
       	 	$result = db('member')->where('id',$info_user['id'])->update($parm);
	        if($result){
				return json(['code'=>200,'msg'=>'申请成功']);
	        }else{
	       		return json(['code'=>402,'msg'=>'申请失败']);
	        }
		}
		
		$userinfo = db('member')->where('id',$info_user['id'])->find();
		$this->assign('is_city',$userinfo['is_city']);
		return  $this->fetch();
	}
	
	//会员直推佣金
	function ztyj(){
		$user_info = session("user_info");
		$w['user_id'] = $user_info['id'];
		$config = get_config('ticket_config');
		$w['type'] = ['in','6,10,14'];
		if($_POST){
			$list = db('gdyj_record')->field('id,message,type,balance,add_time')->where($w)->order("add_time desc")->page(input("page"),10)->select();
			return json(['list'=>$list]);
		}
		$field = "count(id) as c,sum(balance) s";
		$log = db('gdyj_record')->where($w)->field($field)->find();
		if(!$log){
			$log['c'] = 0;
			$log['s'] = 0;
		}
		$this->assign("config",$config);
		$this->assign('log',$log);
		return $this->fetch('');
	}
	
	//会员间推佣金
	function jtyj(){
		$user_info = session("user_info");
		$w['user_id'] = $user_info['id'];
		$config = get_config('ticket_config');
		$w['type'] = ['in','7,11,15'];
		if($_POST){
			$list = db('gdyj_record')->field('id,message,type,balance,add_time')->where($w)->order("add_time desc")->page(input("page"),10)->select();
			return json(['list'=>$list]);
		}
		$field = "count(id) as c,sum(balance) s";
		$log = db('gdyj_record')->where($w)->field($field)->find();
		if(!$log){
			$log['c'] = 0;
			$log['s'] = 0;
		}
		$this->assign("config",$config);
		$this->assign('log',$log);
		return $this->fetch('');
	}
	
	//收款二维码
    public function skewm()
    {	
    	//二维码链接
    	$code_url = "https://".$_SERVER['HTTP_HOST']."/index/member/regist?fromuid=".session('user_info')['id'];
		$use = db("member")->where("open_id",session("openid"))->find();
		$this->assign('logo',$use['logo']);
		$this->assign('code_url',$code_url);
		return  $this->fetch();return  $this->fetch();
    }
	
	//团队和业绩  我的奖励金
	function tdjlj(){
		$page=1;
		$w['user_id'] = session("user_info")['id'];
		$w['type'] = 10;
		if($_POST){
			if(input("page")){
				$page=input("page");
			}
			$begin = ($page-1)*20;
			$agent_info = db('hb_list')->field('id,message,balance,add_time')->where($w)->order("add_time desc")->limit($begin,20)->select();


			return json(['list'=>$agent_info]);
		}
		$sum = db('hb_list')->where($w)->sum('balance');
		if(!$sum){
			$sum = 0;
		}
		return $this->fetch('',['sum'=>$sum]);
	}
	
	//团队和业绩  直接推荐佣金
	function tdzjyj(){
		$page=1;
		$w['user_id'] = session("user_info")['id'];
		$w['type'] = 11;
		if($_POST){
			if(input("page")){
				$page=input("page");
			}
			$begin = ($page-1)*20;
			$agent_info = db('hb_list')->field('id,message,balance,add_time')->where($w)->order("add_time desc")->limit($begin,20)->select();


			return json(['list'=>$agent_info]);
		}
		$sum = db('hb_list')->where($w)->sum('balance');
		if(!$sum){
			$sum = 0;
		}
		return $this->fetch('',['sum'=>$sum]);
	}
	
	//团队和业绩  间接推荐佣金
	function tdjjyj(){
		$page=1;
		$w['user_id'] = session("user_info")['id'];
		$w['type'] = 12;
		if($_POST){
			if(input("page")){
				$page=input("page");
			}
			$begin = ($page-1)*20;
			$agent_info = db('hb_list')->field('id,message,balance,add_time')->where($w)->order("add_time desc")->limit($begin,20)->select();


			return json(['list'=>$agent_info]);
		}
		$sum = db('hb_list')->where($w)->sum('balance');
		if(!$sum){
			$sum = 0;
		}
		return $this->fetch('',['sum'=>$sum]);
	}
	
	//团队和业绩  合伙人佣金
	function tdptyj(){
		$page=1;
		$w['user_id'] = session("user_info")['id'];
		$w['type'] = 13;
		if($_POST){
			if(input("page")){
				$page=input("page");
			}
			$begin = ($page-1)*20;
			$agent_info = db('hb_list')->field('id,message,balance,add_time')->where($w)->order("add_time desc")->limit($begin,20)->select();


			return json(['list'=>$agent_info]);
		}
		$sum = db('hb_list')->where($w)->sum('balance');
		if(!$sum){
			$sum = 0;
		}
		return $this->fetch('',['sum'=>$sum]);
	}
	
	function set_member(){
		
		return $this->fetch();
	}
	
	function address_list(){
		$address_list = db('member_address')->where('user_id',session('user_info')['id'])->select();
		
		$this->assign('address_list',$address_list);
		return $this->fetch();
		
	}
	
	function address_add() {
		if(request()->isPost()) {
			$data = input('post.');
			
			$result = true;
			Db::startTrans();
			//检查用户是否有地址
			$address = db('member_address')->where('user_id',session('user_id'))->find();
			if(!$address){
				$data['is_default'] = 1;
			}else if(!isset($data['is_default'])){
				
				$rest1 = db('member_address')
						->where('user_id', session('user_id'))
						->update(['is_default' => 0]);
						
				if(!$rest1) $result=false;
				$data['is_default'] = 1;
			}
			
			$data['user_id'] = session('user_id');
			$data['add_time'] = now_time();
			$rest2 = db('member_address')->insert($data);
			if(!$rest2) $result=false;
			

			if($result){
				Db::commit();
				return  json(["code"=>200,"msg"=>"添加成功",'url'=>url('member/address_list')]);
			}else{
				Db::rollback();
				return  json(["code"=>403,"msg"=>"添加失败"]);
			}
		}
		
		return $this->fetch();
	}
	
	function address_default(){
		if(request()->isPost()) {
			$address_id = input('address_id');
			
			if (!is_numeric($address_id)) {
				return  json(["code"=>405,"msg"=>"数据异常"]);
			}
			
			$rest1 = db('member_address')
						->where('user_id', session('user_id'))
						->update(['is_default' => 0]);
			if($rest1){
				$rest2 = db('member_address')
						->where('id', $address_id)
						->where('user_id', session('user_id'))
						->update(['is_default' => 1]);
				if($rest2){
					return  json(["code"=>200,"msg"=>"修改成功"]);
				}else{
					return  json(["code"=>403,"msg"=>"修改失败"]);
				}
			}else{
				return  json(["code"=>405,"msg"=>"修改失败"]);
			}
		}
	}
	
	
	
	
	function address_update() {
		$address_id = input('address_id');
		if (!is_numeric($address_id)) {
			return  json(["code"=>405,"msg"=>"数据异常"]);
		}
		$address_info = db('member_address')->where('id', $address_id)->where('user_id', session('user_id'))->find();

		$this->assign('address_info',$address_info);
		
		if(request()->isPost()) {
			$data = input('post.');
			unset($data['address_id']);
			$data['add_time'] = now_time();
			
			$result = true;
			Db::startTrans();
			
			//检查是否默认地址
			if($data['is_default'] == 1){
				//取消所有默认地址
				$rest1 = db('member_address')
						->where('user_id', session('user_id'))
						->update(['is_default' => 0]);
				if(!$rest1) $result=false;
			}
			
			$rest2 = db('member_address')
						->where('id', $address_id)
						->where('user_id', session('user_id'))
						->update($data);
			if(!$rest2) $result=false;
			
			if($result){
				Db::commit();
				return  json(["code"=>200,"msg"=>"修改成功",'url'=>url('member/address_list')]);
			}else{
				Db::rollback();
				return  json(["code"=>403,"msg"=>"修改失败"]);
			}
		}
		return $this->fetch();
	}
	function address_del() {
		if(request()->isPost()) {
			$address_id = input('address_id');
			
			if (!is_numeric($address_id)) {
				return  json(["code"=>405,"msg"=>"数据异常"]);
			}
			
			$result = db('member_address')->where('id', $address_id)->where('user_id', session('user_id'))->delete();
			
			if($result) {
				return  json(["code"=>200,"msg"=>"删除成功"]);
			} else {
				return  json(["code"=>403,"msg"=>"删除失败"]);
			}
		}
	}

	//用户订单
	function orders() {
		$user=session('user_info');
		if($user['is_city']==1){
			$where['ct_id']=session('user_id');
		}elseif($user['is_pt']==1){
			$where['pt_id'] = session('user_id');		
		}else{
			$where['user_id'] = session('user_id');
		}
		if(input('status')>0){
			$where['status'] = input('status');
		}
		
		//获取用户订单信息
		$order_list = db('good_order')
						->where($where)
						->order('add_time desc')
						->limit(10)
						->select();
		foreach($order_list as $key=>&$value){
			//获取订单详情
			$order_item = db('order_items')->where('order_id',$value['order_id'])->select();
			$value['order_item'] = $order_item;
		}
		
		$this->assign('status',input('status'));
		$this->assign('order_list',$order_list);
		return $this->fetch();
	}
	
	//订单详情
	function order_page(){
		
		//获取用户订单信息
		$order_info = db('good_order')
						->where('order_id',input('order_id'))
						->find();
		if($order_info){
			$order_item = db('order_items')->where('order_id',$order_info['order_id'])->select();
			
			$order_info['order_item'] = $order_item;
			
		}else{
			return $this->fetch('none_style');
		}
		
		$this->assign('order_info',$order_info);
		return $this->fetch();
	}
	
	//关闭订单
	function cancel_order(){
		if(request()->isAjax()){
			$order_info = db('good_order')->where('order_id',input('order_id'))->where('user_id',session('user_id'))->find();
			if($order_info){
				 $result = db('good_order')->where('order_id',$order_info['order_id'])->update(['status'=>5]);
				 if($result){
				 	return json(['code'=>200,'msg'=>'订单已取消']);
				 }else{
				 	return json(['code'=>403,'msg'=>'订单取消失败']);
				 }
			}else{
				return json(['code'=>403,'msg'=>'订单不存在，数据异常']);
			}
		}
	}
	
	//确认收货
	function take_order(){
		if(request()->isAjax()){
				Db::startTrans();	
				$result = db('good_order')->where('order_id',input("order_id"))->where('user_id',session('user_id'))->update(['status'=>4]);
				if(!$result){
					Db::rollback();
				 	return json(['code'=>400,'msg'=>'收货失败']);
				}
				$order_info = db('good_order')->where('order_id',input("order_id"))->where('user_id',session('user_id'))->find();
				$result = db('member')->where('id', $order_info['user_id'])->setInc('total_xf', $order_info['pay_price']);
				if(!$result){
					Db::rollback();
				 	return json(['code'=>400,'msg'=>'收货失败']);
				}
				//获取第一上级用户信息
				$user_pid1 = db('member')->where('id',$order_info['tj_id'])->find();	
				if($user_pid1&&$order_info['tj_yj']){
					$rest3 = gdyj_record_log($user_pid1['id'],$order_info['tj_yj'],"直推会员预定产品奖励",14,$order_info['order_sn'],$order_info['pt_id'],$order_info['ct_id'],session('user_id'));
					if(!$rest3) {
						Db::rollback();
				 		return json(['code'=>400,'msg'=>'收货失败']);
					}else{
						$payment=new PaymentController();
						$payment->send_pay($user_pid1['open_id'],$order_info['order_sn'],$order_info['tj_yj'],'直推会员预定产品奖励');
					}
				}
				//获取第二上级用户信息
				$user_pid2 = db('member')->where('id',$order_info['tj_id2'])->find();
				if($user_pid2&&$order_info['tj_yj2']){
		
					$rest5 = gdyj_record_log($user_pid2['id'],$order_info['tj_yj2'],"间推会员购买产品奖励",15,$order_info['order_sn'],$order_info['pt_id'],$order_info['ct_id'],session('user_id'));
					if(!$rest5){
						Db::rollback();
				 		return json(['code'=>400,'msg'=>'收货失败']);
					}else{
						$payment=new PaymentController();
						$payment->send_pay($user_pid2['open_id'],$order_info['order_sn'],$order_info['tj_yj2'],'间推会员购买产品奖励');
					}
				}
		
				//获取合伙人信息
				$user_pt = db('member')->where('id',$order_info['pt_id'])->find();
				if($user_pt&&$order_info['pt_yj']){
					//计算第一上级累计佣金
					$user_pt_total = bcadd($user_pt['total_yj'], $order_info['pt_yj'], 2);
					//计算待提现佣金
					$user_pt_dtx_yj = bcadd($user_pt['yj'], $order_info['pt_yj'], 2);
					//更新第一级用户信息
					$result = db('member')->where('id', $user_pt['id'])->update(['total_yj'=>$user_pt_total,'yj'=>$user_pt_dtx_yj,'update_time'=>now_time()]);
					if(!$result){
						Db::rollback();
				 		return json(['code'=>400,'msg'=>'收货失败']);
					}
					//佣金变化日志
					$result = gdyj_record_log($user_pt['id'],$order_info['pt_yj'],"会员购买产品合伙人奖励",16,$order_info['order_sn'],$order_info['pt_id'],$order_info['ct_id'],session('user_id'));
					if(!$result){
						Db::rollback();
				 		return json(['code'=>400,'msg'=>'收货失败']);
					}
					
				}
		
				//获取运营中心信息
				$user_city = db('member')->where('id',$order_info['ct_id'])->find();
				if($user_city){
					//计算第一上级累计佣金
					$user_city_total = bcadd($user_city['total_yj'], $order_info['ct_yj'], 2);
					//计算待提现佣金
					$user_city_dtx_yj = bcadd($user_city['yj'], $order_info['ct_yj'], 2);
					//更新第一级用户信息
					$rest8 = db('member')->where('id', $user_city['id'])->update(['total_yj'=>$user_city_total,'yj'=>$user_city_dtx_yj,'update_time'=>now_time()]);
									
					if(!$rest8) $result =false;
					//佣金变化日志
					$result = gdyj_record_log($user_city['id'],$order_info['ct_yj'],"购买产品运营中心奖励",17,$order_info['order_sn'],0,$order_info['ct_id'],session('user_id'));
					if(!$result){
						Db::rollback();
				 		return json(['code'=>400,'msg'=>'收货失败']);
					}
				}
				Db::commit();
				 return json(['code'=>200,'msg'=>'确认收货成功']);
				
			
		}
	}
	function fahuo(){
		if(request()->isAjax()){
			$up['shipping_code']=input("no");
			$up['status']=3;
			$up['shipping_time']=now_time();
			 $result = db('good_order')->where('order_id',input('order_id'))->update($up);
			 if($result){
			 	return json(['code'=>200,'msg'=>'发货成功']);
			 }else{
			 	return json(['code'=>403,'msg'=>'发货失败']);
			 }
			
		}
	}
	//我的美丽家信息
	public function home_list(){
		if($_POST){
			$page = 1;
			if(input("page")){
				$page=input("page");
			}
			$begin=($page-1)*10;
			$home = db('beaut_home')->field('id,home_name,add_time')->order('add_time desc')->limit($begin,10)->select();
			
			if($home){
				return json(['code'=>200,"list"=>$home]);
			}else{
				return json(['list'=>null]);
			}
		}
		return $this->fetch();
	}
	
	//我的美丽家信息详情
	public function home_page(){
		$home_info = db('beaut_home')->field('id,home_name,sale_price,thumb,unit,content')->where('id',input('id'))->find();			
		$this->assign('list', $home_info);
		return $this->fetch();
	}
	
	//我的访问信息
	public function visit_list(){
		if($_POST){
			$info_user = session('user_info');
			$page = 1;
			$where['user_id'] = $info_user['id'];
			if(input("page")){
				$page=input("page");
			}
			$begin=($page-1)*10;
			$visit = db('member')->field('id,logo,mobile,real_name,nick_name,add_time,is_pt')->where("pid1",$info_user['id'])->order('add_time desc')->limit($begin,10)->select();
			
			if($visit){
				return json(['code'=>200,"list"=>$visit]);
			}else{
				return json(['list'=>null]);
			}
		}
		return $this->fetch();
	}

	//我的会员卡详情	
	public function card_page(){
		$type = input('type');
		$card_name = '';
		if($type=='hyqy'){
			$card_name = '会员权益';
		}elseif($type=='syxz'){
			$card_name = '使用须知';
		}elseif($type=='grjf'){
			$card_name = '个人积分';
		}else{
			$card_name = '联系我们';
		}
		
		$card_config = Db::name('system')->field('value')->where('name', 'card_config')->find();
	    $card_config = unserialize($card_config['value']);
		$this->assign('card_name', $card_name);
		$this->assign('card_config', $card_config[$type]);
		return $this->fetch();
	}
	
	public function zyj(){
		$user_id = session("user_info")['id'];;		
		$w['user_id'] = $user_id;
		if($_POST){
			$page = input("page");
			$begin = ($page-1)*20;
			$field="user_id,add_time,balance";
			$agent_info = db('gdyj_record')->where($w1)->order("add_time desc")->limit($begin,20)->select();
			return json(['list'=>$agent_info]);
		}
		$is_city=session("user_info")['is_city'];
		$is_pt=session("user_info")['is_pt'];
		
		$where['user_id'] = $user_id;
		$where['type']=['neq',18];
		$zyj=db('gdyj_record')->where($where)->sum('balance');
		$this->assign('zyj', $zyj);
		
		$where1['user_id'] = $user_id;
		$where1['type'] = ['in','6,10,14'];
		$ztyj=db('gdyj_record')->where($where1)->sum('balance');
		$this->assign('ztyj', $ztyj);

		$where2['user_id'] = $user_id;
		$where2['type'] = ['in','7,11,15'];
		$jtyj=db('gdyj_record')->where($where2)->sum('balance');
		$this->assign('jtyj', $jtyj);
		
		
		if($is_city==1){
			$where3['user_id'] = $user_id;
			$where3['type'] = ['in','8,12,16'];
			$ptyj=db('gdyj_record')->where($where3)->sum('balance');
			$this->assign('ptyj', $ptyj);
			
			$where5['user_id'] = $user_id;
			$where5['type'] =9;
			$tjptyj=db('gdyj_record')->where($where5)->sum('balance');
			$this->assign('tjptyj', $tjptyj);
			
			
			
			$where5['user_id'] = $user_id;
			$where5['type'] =20;
			$tjctyj1=db('gdyj_record')->where($where5)->sum('balance');
			$this->assign('tjctyj1', $tjctyj1);
	
			$where5['type'] =19;
			$tjctyj=db('gdyj_record')->where($where5)->sum('balance');
			$this->assign('tjctyj', $tjctyj);
			
			$where4['user_id'] = $user_id;
			$where4['type'] = ['in','5,13,17'];
			$ctyj=db('gdyj_record')->where($where4)->sum('balance');
			$this->assign('ctyj', $ctyj);
		}elseif($is_pt){
			$where3['user_id'] = $user_id;
			$where3['type'] = ['in','8,12,16'];
			$ptyj=db('gdyj_record')->where($where3)->sum('balance');
			$this->assign('ptyj', $ptyj);
			
			$where5['user_id'] = $user_id;
			$where5['type'] =9;
			$tjptyj=db('gdyj_record')->where($where5)->sum('balance');
			$this->assign('tjptyj', $tjptyj);
		}
				
		return $this->fetch();		
		
	}
	
	//充值
	function chongzhi(){
		
		$user_info = db('member')->where('id',session('user_id'))->find();
		$user_info['order_sn']  = order_create_no();
		$this->assign('user_info', $user_info);		
		
		$recharge_config = Db::name('system')->field('value')->where('name', 'recharge_config')->find();
	    $recharge_config = unserialize($recharge_config['value']);
		$this->assign('recharge_config', $recharge_config);	
		return $this->fetch();		
	}
	
}
